import { ForexDanaClassifier } from '../services/ForexDanaClassifier';
import { ExtractionResult } from '../types';
import * as fs from 'fs';
import * as path from 'path';

/**
 * ForexDana翻译分类示例
 * 读取提取结果，进行分类并生成报告
 */
async function classifyForexDanaTranslations() {
  try {
    // 1. 读取翻译提取结果
    console.log('读取翻译提取结果...');
    
    // 可以从参数或环境变量获取文件路径
    const resultFilePath = process.argv[2] || process.env.RESULT_FILE_PATH || './extraction-result.json';
    
    if (!fs.existsSync(resultFilePath)) {
      throw new Error(`结果文件不存在: ${resultFilePath}`);
    }
    
    const resultContent = fs.readFileSync(resultFilePath, 'utf-8');
    const extractionResult: ExtractionResult = JSON.parse(resultContent);
    
    // 2. 使用分类器进行分类
    console.log(`开始对${extractionResult.translations.length}条翻译进行分类...`);
    const classifier = new ForexDanaClassifier();
    const classificationResult = classifier.classify(extractionResult.translations);
    
    // 3. 生成分类报告
    console.log('生成分类报告...');
    const report = classifier.generateReport(classificationResult);
    
    // 4. 保存分类结果
    console.log('保存分类结果...');
    const outputDir = path.dirname(resultFilePath);
    
    // 保存分类报告
    fs.writeFileSync(
      path.join(outputDir, 'classification-report.txt'),
      report,
      'utf-8'
    );
    
    // 保存完整分类结果
    fs.writeFileSync(
      path.join(outputDir, 'classified-translations.json'),
      JSON.stringify(classificationResult, null, 2),
      'utf-8'
    );
    
    // 为每个模块创建单独的JSON文件
    
    // 基础设施类
    const infrastructureDir = path.join(outputDir, 'infrastructure');
    if (!fs.existsSync(infrastructureDir)) {
      fs.mkdirSync(infrastructureDir, { recursive: true });
    }
    
    // 保存每个基础设施模块的翻译
    for (const [dir, translations] of Object.entries(classificationResult.infrastructureTranslations)) {
      if (translations.length > 0) {
        fs.writeFileSync(
          path.join(infrastructureDir, `${dir}.json`),
          JSON.stringify(translations, null, 2),
          'utf-8'
        );
      }
    }
    
    // 业务功能类
    const businessDir = path.join(outputDir, 'business');
    if (!fs.existsSync(businessDir)) {
      fs.mkdirSync(businessDir, { recursive: true });
    }
    
    // 保存每个业务模块的翻译
    for (const [module, translations] of Object.entries(classificationResult.businessTranslations)) {
      if (translations.length > 0) {
        fs.writeFileSync(
          path.join(businessDir, `${module}.json`),
          JSON.stringify(translations, null, 2),
          'utf-8'
        );
      }
    }
    
    // 保存未分类的翻译
    if (classificationResult.unclassifiedTranslations.length > 0) {
      fs.writeFileSync(
        path.join(outputDir, 'unclassified.json'),
        JSON.stringify(classificationResult.unclassifiedTranslations, null, 2),
        'utf-8'
      );
    }
    
    console.log('分类完成!');
    console.log(`分类报告已保存到: ${path.join(outputDir, 'classification-report.txt')}`);
    console.log(`基础设施类翻译已保存到: ${infrastructureDir}`);
    console.log(`业务功能类翻译已保存到: ${businessDir}`);
    
    // 打印摘要
    console.log('\n分类摘要:');
    console.log(`总翻译条目: ${classificationResult.statistics.totalCount}`);
    console.log(`基础设施类: ${classificationResult.statistics.infrastructureCount} (${((classificationResult.statistics.infrastructureCount / classificationResult.statistics.totalCount) * 100).toFixed(1)}%)`);
    console.log(`业务功能类: ${classificationResult.statistics.businessCount} (${((classificationResult.statistics.businessCount / classificationResult.statistics.totalCount) * 100).toFixed(1)}%)`);
    console.log(`未分类条目: ${classificationResult.statistics.unclassifiedCount} (${((classificationResult.statistics.unclassifiedCount / classificationResult.statistics.totalCount) * 100).toFixed(1)}%)`);
    
  } catch (error) {
    console.error('翻译分类过程中出错:', error);
    process.exit(1);
  }
}

// 执行分类
classifyForexDanaTranslations().catch(console.error); 